package 山形数组的不重复排序;
import java.util.LinkedList;
import java.util.Stack;

public class Main
{
    public static void main(String[] args)
    {
        int[] test = new int[]{1, 2, 2, 5, 10, 11, 8, 3, 1, 1};
        LinkedList<Integer> list = new LinkedList<Integer>();
        for (int i = 0; i < test.length; i++)
        {
            list.addLast(test[i]);
        }
        Main m = new Main();
        test = m.GetArray(list);
        for (int i = 0; i < test.length; i++)
        {
            System.out.println(test[i]);
        }
    }
    public int[] GetArray(LinkedList<Integer> list)
    {
        //去重
        int maxnum = 0;
        for (int i = 1; i < list.size(); i++)
        {
            if (list.get(i - 1).equals(list.get(i)))
            {
                list.remove(i);
                i--;
            }
        }
        //最大数
        for (int i = 1; i < list.size(); i++)
        {
            if (list.get(i - 1) > list.get(i) && list.get(i - 1) > maxnum)
            {
                maxnum = list.get(i - 1);
            }
        }
        Stack<Integer> stack = new Stack<Integer>();
        while (!list.peekLast().equals(maxnum))
        {
            if (list.peekFirst().equals(list.peekLast()))
                list.removeLast();
             else if (list.peekFirst() > list.peekLast())
                list.addLast(list.pollLast());
             else
            {
                while (list.peekFirst() < list.peekLast())
                    stack.push(list.pollFirst());
                if (list.peekFirst() == list.peekLast())
                    list.removeLast();
                else
                    list.addFirst(list.pollLast());
            }
        }
        while (!stack.isEmpty())
            list.addFirst(stack.pop());
        int[] res = new int[list.size()];
        for (int i = 0; i < res.length; i++)
        {
            res[i] = list.poll();
        }
        return res;
    }
}

